/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.user.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.customize.enums.AbnormalEnum;
import com.pig4cloud.pigx.common.customize.exception.DuckweedException;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.user.dto.AppTransferPageDTO;
import com.pig4cloud.pigx.user.dto.TransferDTO;
import com.pig4cloud.pigx.user.dto.TransferDetailedPageDTO;
import com.pig4cloud.pigx.user.dto.UserTransferDTO;
import com.pig4cloud.pigx.user.entity.TransferDetailed;
import com.pig4cloud.pigx.user.enums.TransferEnums;
import com.pig4cloud.pigx.user.mapper.TransferDetailedMapper;
import com.pig4cloud.pigx.user.service.TransferDetailedService;
import com.pig4cloud.pigx.user.vo.AdminPageDemoVO;
import com.pig4cloud.pigx.user.vo.LoginUserTransferVO;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 划转记录i
 *
 * @author pigx code generator
 * @date 2022-11-28 15:30:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/transfer_detailed" )
@Api(value = "transfer_detailed", tags = "划转记录i管理")
public class TransferDetailedController {

    private final  TransferDetailedService transferDetailedService;

	private final TransferDetailedMapper mapper;

    /**
     * 分页查询
     * @return r
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    public R<AdminPageDemoVO<TransferDetailed>> getTransferDetailedPage(@RequestBody TransferDetailedPageDTO dto) {
		try {
			Page<TransferDetailed> iPage = mapper.selectAdminPage(new Page<TransferDetailed>() {{
				setCurrent(dto.getCurrent());
				setSize(dto.getSize());
			}}, dto);
			for (TransferDetailed record : iPage.getRecords()) {
				record.setType(TransferEnums.Type.findName(record.getType()));
			}
			TransferDetailed detailed = mapper.selectAdminStatistics(dto);
			return R.ok(new AdminPageDemoVO<TransferDetailed>(){{
				setPage(iPage);
				setStatistics(detailed);
			}});
		} catch (Exception e) {
			throw new DuckweedException(AbnormalEnum.MSG.DISCONNECTED);
		}
    }

	/**
	 * 获取用户划转记录 分页
	 * @param dto dto
	 * @return r
	 */
	@PostMapping("/app/page")
	public R<Page<LoginUserTransferVO>> getAppPage(@RequestBody AppTransferPageDTO dto) {
		return transferDetailedService.getAppPage(dto);
	}

	/**
	 * 账户划转
	 * @param dto dto
	 * @return r
	 */
	@ApiOperation(value = "账户划转", notes = "账户划转")
	@PostMapping()
	public R<String> transfer(@RequestBody TransferDTO dto) {
		return transferDetailedService.transfer(dto);
	}

	/**
	 * 用户燃料转账
	 * @param dto dto
	 * @return r
	 */
	@ApiOperation(value = "用户燃料转账", notes = "用户燃料转账")
	@PostMapping("/transaction")
	public R<String> userTransfer(@RequestBody UserTransferDTO dto) {
		return transferDetailedService.userTransfer(dto);
	}


    /**
     * 通过id查询划转记录i
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_transferdetailed_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(transferDetailedService.getById(id));
    }

    /**
     * 修改划转记录i
     * @param transferDetailed 划转记录i
     * @return R
     */
    @ApiOperation(value = "修改划转记录i", notes = "修改划转记录i")
    @SysLog("修改划转记录i" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_transferdetailed_edit')" )
    public R updateById(@RequestBody TransferDetailed transferDetailed) {
        return R.ok(transferDetailedService.updateById(transferDetailed));
    }

    /**
     * 通过id删除划转记录i
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除划转记录i", notes = "通过id删除划转记录i")
    @SysLog("通过id删除划转记录i" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_transferdetailed_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(transferDetailedService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param transferDetailed 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('user_transferdetailed_export')" )
    public List<TransferDetailed> export(TransferDetailed transferDetailed) {
        return transferDetailedService.list(Wrappers.query(transferDetailed));
    }
}
